package com.example.alex_zhang.myapplication;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class MainActivity extends AppCompatActivity {
    GifImageDecoder gifDecoder;
    private LinearLayout llParent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         llParent = (LinearLayout) findViewById(R.id.ll_parent);
    }

    public void decoder(View view) {
        long l = SystemClock.currentThreadTimeMillis();
        try {
            gifDecoder = new GifImageDecoder();
            Bitmap bitmap = gifDecoder.readFirstFrame(this.getResources().openRawResource(R.raw.test));
            if (bitmap == null) {
                return;
            }
            ImageView imageView = new ImageView(this);
            imageView.setLayoutParams(new LinearLayout.LayoutParams(200, 200));
            imageView.setImageBitmap(bitmap);
            llParent.addView(imageView);
        } catch (IOException e) {
            e.printStackTrace();
        }

        Log.d("MainActivity", "decoder: "+(SystemClock.currentThreadTimeMillis()-l));
//        showGif2();
//        String s = Environment.getExternalStorageDirectory().getPath() + "/e_a";
//        MediaMetadataRetriever media = new MediaMetadataRetriever();
//        media.setDataSource(s);
//        Bitmap frameAtTime = media.getFrameAtTime();
//
//
//
//        long l = SystemClock.currentThreadTimeMillis();
//        // showGif2();
//        InputStream inputStream = this.getResources().openRawResource(R.raw.e_a);
//        Movie movie = Movie.decodeStream(inputStream);
//        Log.d("GifImageDecoder", "decoder: " + (SystemClock.currentThreadTimeMillis() - l));
//        BitmapFactory.Options options = new BitmapFactory.Options();
//        options.inMutable = true;
//        options.inPreferredConfig = Bitmap.Config.RGB_565;
//
//
//        Bitmap bitmap = Bitmap.createBitmap(movie.width(), movie.height(), Bitmap.Config.ARGB_8888);



    }

    void showGif2() {
        gifDecoder = new GifImageDecoder();
        try {
            gifDecoder.read(this.getResources().openRawResource(R.raw.e_a));

            ImageView iv_image = new ImageView(MainActivity.this);
            iv_image.setPadding(5, 5, 5, 5);
            ViewGroup.LayoutParams lparams = new ViewGroup.LayoutParams(300, 300);
            iv_image.setLayoutParams(lparams);
            iv_image.setImageBitmap(gifDecoder.getFrame(0));
            llParent.addView(iv_image);


            //            int size = gifDecoder.getFrameCount();
            //            for (int i = 0; i < size; i++) {
            //                ImageView iv_image = new ImageView(MainActivity.this);
            //                iv_image.setPadding(5, 5, 5, 5);
            //                ViewGroup.LayoutParams lparams = new ViewGroup.LayoutParams(100, 100);
            //                iv_image.setLayoutParams(lparams);
            //                iv_image.setImageBitmap(gifDecoder.getFrame(i));
            //                llParent.addView(iv_image);
            //                //                gifFrame.nextFrame();
            //            }
        } catch (Resources.NotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


    public byte[] fileConnect(InputStream is) {
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            int ch = 0;
            while ((ch = is.read()) != -1) {
                baos.write(ch);
            }
            byte[] b = baos.toByteArray();
            baos.close();
            baos = null;
            is.close();
            is = null;
            return b;
        } catch (Exception e) {
            return null;
        }
    }
}
